package com.ruoyi.teaching.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.domain.YaCourseDesignDetail;
import com.ruoyi.teaching.mapper.YaCourseDesignDetailMapper;
import com.ruoyi.teaching.service.IYaCourseDesignDetailService;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.mapper.YaCourseMapper;
import com.ruoyi.teaching.domain.YaCourse;
import com.ruoyi.teaching.service.IYaCourseService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@Service
public class YaCourseServiceImpl implements IYaCourseService {
    @Autowired
    private YaCourseMapper yaCourseMapper;
    @Autowired
    private IYaCourseDesignDetailService yaCourseDesignDetailService;

    @Autowired
    private YaCourseDesignDetailMapper yaCourseDesignDetailMapper;
    /**
     * 查询课程
     *
     * @param id 课程主键
     * @return 课程
     */
    @Override
    public YaCourse selectYaCourseById(Long id) {
        return yaCourseMapper.selectYaCourseById(id);
    }

    /**
     * 查询课程列表
     *
     * @param yaCourse 课程
     * @return 课程
     */
    @Override
    public List<YaCourse> selectYaCourseList(YaCourse yaCourse) {
        return yaCourseMapper.selectYaCourseList(yaCourse);
    }

    /**
     * 新增课程
     *
     * @param yaCourse 课程
     * @return 结果
     */
    @Override
    public int insertYaCourse(YaCourse yaCourse) {
        yaCourse.setCreateTime(DateUtils.getNowDate());
        return yaCourseMapper.insertYaCourse(yaCourse);
    }

    /**
     * 修改课程
     *
     * @param yaCourse 课程
     * @return 结果
     */
    @Override
    public int updateYaCourse(YaCourse yaCourse) {
        yaCourse.setUpdateTime(DateUtils.getNowDate());
        return yaCourseMapper.updateYaCourse(yaCourse);
    }

    /**
     * 批量删除课程
     *
     * @param ids 需要删除的课程主键
     * @return 结果
     */
    @Override
    public int deleteYaCourseByIds(Long[] ids) {
        return yaCourseMapper.deleteYaCourseByIds(ids);
    }

    /**
     * 删除课程信息
     *
     * @param id 课程主键
     * @return 结果
     */
    @Override
    public int deleteYaCourseById(Long id) {
        return yaCourseMapper.deleteYaCourseById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertYaCourseDesignDetail(Integer stage, Long courseId, List<YaCourseDesignDetail> courseDesignDetailList) {
        //1.先删除
        List<Long> idList = courseDesignDetailList.stream().filter(dd -> ObjectUtils.isNotEmpty(dd.getId())).map(YaCourseDesignDetail::getId).collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(idList)){
            yaCourseDesignDetailMapper.deleteYaCourseDesignDetailByNotInId(courseId,stage,idList);
        }
        //区分新增和修改的集合
        Date createTime = new Date();
        String username = SecurityUtils.getUsername();
        ArrayList<YaCourseDesignDetail> addList = new ArrayList<>(courseDesignDetailList.size());

        ArrayList<YaCourseDesignDetail> updateList = new ArrayList<>(courseDesignDetailList.size());
        courseDesignDetailList.forEach(cdd->{
            if (ObjectUtils.isEmpty(cdd.getId())) {
                addList.add(cdd);
            } else {
                updateList.add(cdd);
            }
        });

        for (YaCourseDesignDetail yaCourseDesignDetail : addList) {
            yaCourseDesignDetail.setCreateTime(createTime);
            yaCourseDesignDetail.setCreateBy(username);
            yaCourseDesignDetail.setCourseId(courseId);
        }

        for (YaCourseDesignDetail yaCourseDesignDetail : updateList) {
            yaCourseDesignDetail.setUpdateTime(createTime);
            yaCourseDesignDetail.setUpdateBy(username);
        }
        //2.再修改,新增
        boolean result = yaCourseDesignDetailService.saveOrUpdateBatch(courseDesignDetailList, 60);
        return result?1:0;
    }

}
